//
//  HKKeyboardHelper.m
//  KSBridgeDesign
//
//  Created by huzhaoke on 2022/3/12.
//

#import "HKNotificationHandler.h"

NSString * const hHKChangeAvatarNotificationName = @"hHKChangeAvatarNotificationName";

@implementation HKNotificationHandler

- (void)setKeyboardSizeChangeBlock:(void (^)(BOOL show, CGSize size))keyboardSizeChangeBlock {
    if (!_keyboardSizeChangeBlock) {
        _keyboardSizeChangeBlock = keyboardSizeChangeBlock;
        [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWillShow:)
        name:UIKeyboardWillShowNotification
        object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
        
    }
}

- (void)setTextFieldTextDidChangeBlock:(void (^)(void))textFieldTextDidChangeBlock {
    if (!_textFieldTextDidChangeBlock) {
        _textFieldTextDidChangeBlock = textFieldTextDidChangeBlock;
        [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(textFiledEditChanged:)
                           name:@"UITextFieldTextDidChangeNotification"
                         object:nil];
    }
}

- (void)setAvatarDidChangeBlock:(void (^)(void))avatarDidChangeBlock {
    if (!_avatarDidChangeBlock) {
        _avatarDidChangeBlock = avatarDidChangeBlock;
        [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(avatarChanged:)
                       name:hHKChangeAvatarNotificationName
                     object:nil];
    }
}

#pragma mark - block
- (void)keyboardWillShow:(NSNotification *)notification {
    if (self.keyboardSizeChangeBlock) {
        NSDictionary *kbInfo=[notification userInfo];
         CGSize kbSize=[[kbInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
        self.keyboardSizeChangeBlock(YES, kbSize);
    }
}

- (void)keyboardWillHidden:(NSNotification *)notification {
    if (self.keyboardSizeChangeBlock) {
        self.keyboardSizeChangeBlock(NO, CGSizeZero);
    }
}

- (void)textFiledEditChanged:(NSNotification *)notification {
    if (self.textFieldTextDidChangeBlock) {
        self.textFieldTextDidChangeBlock();
    }
}

- (void)avatarChanged:(NSNotification *)notification {
    if (self.avatarDidChangeBlock) {
        self.avatarDidChangeBlock();
    }
}

@end
